<template>
  <!-- 放置弹层组件 -->
  <el-dialog
    :title="ruleForm.id ? '编辑部门' : '新增部门'"
    :visible="showDialog"
    @close="btnCancel"
  >
    <!-- 表单数据 -->
    <el-form ref="deptForm" label-width="120px" :model="ruleForm" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="ruleForm.name" style="width:80%;" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="ruleForm.code" style="width:80%;" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <!-- native修饰符可以找到原生时事件 -->
        <el-select v-model="ruleForm.manager" style="width:80%;" placeholder="请选择" @focus="getEmployeeSimple">
          <el-option v-for="user in username" :key="user.id" :label="user.username" :value="user.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="ruleForm.introduce" style="width:80%;" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- 确定和取消 -->
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" @click="btnCancel">取消</el-button>
        <el-button size="small" type="primary" @click="btnOK">确定</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getDepartments, addDepartments, getDepartmentDetail, updateDepartments } from '@/api/departments'
import { getEmployeeSimple } from '@/api/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    treeNode: {
      type: Object,
      default: null
    }
  },

  data() {
    // 检查部门名称是否重复
    const chechNameRepeat = async(rule, value, callback) => {
      // value部门名称
      const { depts } = await getDepartments()
      let isRepeat = false
      if (this.ruleForm.id) {
        // 数据库中已经有这个数据
        isRepeat = depts.filter(item => item.pid === this.treeNode.pid && item.id !== this.treeNode.id).some(item => item.name === value)
      } else {
        // 没有id就是新增模式
        isRepeat = depts.filter(item => item.pid === this.treeNode.id).some(item => item.name === value)
      }
      // 去找同级部门下有咩有和vale相同的

      //   如果true，表示找到一样的名字
      isRepeat ? callback(new Error(`同级部门下已经存在这个${value}部门了`)) : callback()
    }
    // 检查code
    const checkCodeRepat = async(rule, value, callback) => {
      // 要求编码和所有部门编码不能重复
      const { depts } = await getDepartments()
      let isRepect = false

      if (this.ruleForm.id) {
        //  编辑状态
        isRepect = depts.filter(item => item.id !== this.treeNode.id).some(item => item.code === value)
      } else {
        //  新增状态
        isRepect = depts.some(item => item.code === value && value)
      }

      isRepect ? callback(new Error(`组织架构下已经存在这个${value}编码了`)) : callback()
    }
    return {
      username: [],
      ruleForm: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { trigger: 'blur', min: 1, max: 50, message: '部门名称为1-50个字符' },
          { trigger: 'blur', validator: chechNameRepeat }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码为1-50个字符', trigger: 'blur' },
          { validator: checkCodeRepat, trigger: 'blur' }

        ],
        manager: [{ required: true, message: '负责人不能为空', trigger: 'blur' }],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍为1-300个字符', trigger: 'blur' }
        ]
      }
    }
  },

  methods: {
    async getEmployeeSimple() {
      const result = await getEmployeeSimple()
      this.username = result
    },
    // 获取详情方法
    async getDepartmentDetail(id) {
      this.ruleForm = await getDepartmentDetail(id)
    },
    btnOK() {
      // 手动校验表单
      this.$refs.deptForm.validate(async isOK => {
        if (isOK) {
          // 表单校验通过
          if (!this.ruleForm.id) {
            await addDepartments({ ...this.ruleForm, pid: this.treeNode.id })
            //   告诉父组件,获取最新数据,改变视图变化

            this.$message.success('添加成功')
          } else {
            await updateDepartments(this.ruleForm)
            this.$message.success('修改成功')
          }
          this.$emit('addDepartments')
          this.$emit('addDepts')
          this.$emit('update:showDialog', false)
          this.$refs.deptForm.resetFields()
        }
      })
    },
    btnCancel() {
      this.ruleForm = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
      this.$emit('update:showDialog', false)
      this.$refs.deptForm.resetFields()
    }

  }

}
</script>

<style>

</style>
